<?php
/*
 * Created on May 12, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Order_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;
	/**
	 * @var unknown_type
	 */
	var $fullname  ;
	/**
	 * @var unknown_type
	 */
	var $company  ;
	/**
	 * @var unknown_type
	 */
	var $address  ;
	/**
	 * @var unknown_type
	 */
	var $phone;
	/**
	 * @var unknown_type
	 */
	var $fax;
	/**
	 * @var unknown_type
	 */
	var $email;
	/**
	 * @var unknown_type
	 */
	var $title;
	/**
	 * @var unknown_type
	 */
	var $content;
	/*
	 * @access	public
	 */
	var $recei_date;
	/*
	 * @access	public
	 */
	var $answer_date;
	/*
	 * @access	public
	 */
	var $status;
	/*
	 * @access	public
	 */

	function __construct(){
		parent::__construct();
	}

	function insert($data){
		$date = date('Y-m-d H:i:s', time()); //date('Y-mm-dd h:i:s');
		$sql = "INSERT INTO `gtb_order`
			(
			`buyer_register_user_id`,
			`seller_company_id`,
			`created_date`,
			`expected_deliver_date`,
			`actual_deliver_date`,
			`order_total_amount`,
			`order_status`,
			`process_register_user_id`,
			`s_firstname`,
			`s_lastname`,
			`s_address`,
			`s_city`,
			`s_state`,
			`s_country`,
			`s_phone`,
			`s_note`,
			`last_process_date`,
			`payment`,
			`card_type`,
			`card_account_number`,
			`card_expire_date`,
			`tx`,
			`shipping`,
			`shipping_fee`,
			`tax`,
			`payment_status`)
			VALUES (".
		//intval($data['buyer_register_user_id']).", " .
		$this->db->escape($data['buyer_register_user_id']) . ", " .
		intval($data['seller_company_id']).", " .
		$this->db->escape($data['created_date']) . ", " .
		$this->db->escape($data['expected_deliver_date']) . ", " .
		$this->db->escape($data['actual_deliver_date']) . ", " .
		$data['order_total_amount'].", " .
		intval($data['order_status']) .", " .
		intval($data['process_register_user_id']) .", " .
		$this->db->escape($data['s_firstname']) . ", " .
		$this->db->escape($data['s_lastname']) . ", " .
		$this->db->escape($data['s_address']) . ", " .
		$this->db->escape($data['s_city']) . ", " .
		$this->db->escape($data['s_state']) . ", " .
		$this->db->escape($data['s_country']) . ", " .
		$this->db->escape($data['s_phone']) . ", " .
		$this->db->escape($data['s_note']) . ", " .
		$this->db->escape($data['last_process_date']) . ", " .
		$this->db->escape($data['payment']) . ", " .
		$this->db->escape($data['card_type']) . ", " .
		$this->db->escape($data['card_account_number']) . ", " .
		$this->db->escape($data['card_expire_date']) . ", " .
		$this->db->escape($data['tx']) . ", " .
		$this->db->escape($data['shipping']) . ", " .
		$this->db->escape($data['shipping_fee']) . ", " .
		$this->db->escape($data['tax']) . ", " .
		$this->db->escape($data['payment_status']) 
		.")";
		$this->db->query($sql);
		return $this->db->insert_id();
		//return $this->db->affected_rows();
	}
	
	function update($data){
		$sql = "UPDATE gtb_order
				SET ".
				"`seller_company_id` = " . intval($data['quantity']) . "," .
				"`created_date` = " . $this->db->escape($data['created_date']) .
				"`expected_deliver_date` = " . $this->db->escape($data['expected_deliver_date']) .
				"`actual_deliver_date` = " . $this->db->escape($data['actual_deliver_date']) .
				"`order_total_amount` = " . $this->db->escape($data['order_total_amount']) .
				"`order_status` = " . $this->db->escape($data['order_status']) .
				"`process_register_user_id` = " . intval($data['process_register_user_id']) . "," .
				"`s_firstname` = " . $this->db->escape($data['s_firstname']) .
				"`s_lastname` = " . $this->db->escape($data['s_lastname']) .
				"`s_address` = " . $this->db->escape($data['s_address']) .
				"`s_city` = " . $this->db->escape($data['s_city']) .
				"`s_state` = " . $this->db->escape($data['s_state']) .
				"`s_country` = " . $this->db->escape($data['s_country']) .
				"`s_phone` = " . $this->db->escape($data['s_phone']) .
				"`s_note` = " . $this->db->escape($data['s_note']) .
				"`last_process_date` = " . $this->db->escape($data['last_process_date']) .
				"`payment` = " . $this->db->escape($data['payment']) .
				"`card_type` = " . $this->db->escape($data['card_type']) .
				"`card_account_number` = " . $this->db->escape($data['card_account_number']) .
				"`card_expire_date` = " . $this->db->escape($data['card_expire_date']) .
				"`tx` = " . $this->db->escape($data['tx']) .
				"`shipping` = " . $this->db->escape($data['shipping']) .
				"`shipping_fee` = " . $this->db->escape($data['shipping_fee']) .
				"`tax` = " . $this->db->escape($data['tax']) .
				"`payment_status`= " . intval($data['payment_status']) .		
				" WHERE order_id = " .intval($data['order_id']);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	
	function delete($member_id, $product_id = ''){
		$sql = '';

		if (!empty($product_id))
		$sql = "DELETE FROM gtb_cart WHERE product_id = " .intval($product_id) . ' AND member_id = ' .intval($member_id);
		else
		$sql = "DELETE FROM gtb_cart WHERE member_id = " . intval($member_id);
			
		$this->db->query($sql);

		return $this->db->affected_rows();
	}

	
	function getOrderById($orderId, $userId = 0, $created_date = ''){
		$sql = "";
		
		$sql = "SELECT *
				FROM gtb_order 
				WHERE order_id = ". intval($orderId) . " AND (buyer_register_user_id = " . intval($userId) . " OR 0 = " . intval($userId) . ") 
					AND (created_date =" . $this->db->escape($created_date) . " OR '' = " .  $this->db->escape($created_date) . ")";
			
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}
	
	
	function insertDetail($data){
		
		$sql = "INSERT INTO `gtb_order_detail`
			(
			`order_id`,
			`product_id`,
			`product_price_actual`,
			`product_price_paid`,
			`quantity`
			)
			VALUES (".
		intval($data['order_id']).", " .
		intval($data['product_id']).", " .
		$this->db->escape($data['product_price_actual']) . ", " .
		$this->db->escape($data['product_price_paid']) . ", " .
		intval($data['quantity']) 
		.")";
		$this->db->query($sql);
		//return $this->db->insert_id();
		return $this->db->affected_rows();
	}
	
	

	function getCartMember($member_id, $product_id = 0){
		$sql = "";
		if (empty($product_id))
		$sql = "SELECT c.*, p.vi_name, p.en_name, p.price, p.image, p.date_add
					FROM gtb_cart c
					LEFT JOIN gtb_product p ON c.product_id = p.id_product
					WHERE member_id = ". intval($member_id) ." ORDER BY date_update DESC";
		else
		$sql = "SELECT *
				FROM gtb_cart WHERE member_id = ". intval($member_id) . " AND product_id = " . intval($product_id);
			
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function getAll($offset = 0, $number_record = 10){

		$sql = "SELECT *
				FROM gtb_cart ORDER BY date_add DESC LIMIT $offset, $number_record";

		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();

		return $result;
	}

	function countAll(){
		$result = null;

		$sql = "SELECT COUNT(*) as num_record
					FROM gtb_cart 
				";


		$query = $this->db->query($sql);
		$result = $query->result();

		return $result[0]->num_record;
	}


	function search($offset = 0, $number_record = 30, $memberId = 0){
        $memberId = intval($memberId);
		$sql = "SELECT * 
				FROM gtb_order
				WHERE buyer_register_user_id = " . $memberId . " OR 0 = " . $memberId . "
				ORDER BY order_id DESC
				LIMIT $offset, $number_record";
		
        $query = $this->db->query($sql);
		$result = $query->result_array();
       	
		return $result;
		
//		$sql = "SELECT p.*, p.$name name, u.$name unit, prov.$name provider, s.$name source, $description description, match(p.vi_name, p.en_name, p.vi_description, p.en_description, p.code_pcategory, p.code ) against(".$this->db->escape($keyword)." IN BOOLEAN MODE) as relevance " .
//				" FROM gtb_product p " .
//				" LEFT JOIN  gtb_unit u ON u.id = p.id_unit" .
//				" LEFT JOIN  gtb_source s ON s.id = p.id_source" .
//                " LEFT JOIN  gtb_provider prov ON prov.id_provider = p.id_provider" .
//                " WHERE  (id_pcategory IN (". intval($id_category). ") OR (-1 = " . intval($id_category) . ")) AND  match(p.vi_name, p.en_name, p.vi_description, p.en_description, p.code_pcategory, p.code ) against(".$this->db->escape($keyword)." IN BOOLEAN MODE) " .
//				" ORDER BY relevance DESC ".
//				" LIMIT $offset, $number_record";
		
	}
	
	function countSearch($memberId = 0){
		
        $memberId = intval($memberId);
        
		$sql = "SELECT COUNT(*) as num_record" .
				" FROM gtb_order " .
				" WHERE  buyer_register_user_id = ". $memberId. " OR 0  = " . $memberId;
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	if($result)
			return $result[0]->num_record;
		else return 0;
		
//		$sql = "SELECT COUNT(*) as num_record" .
//				" FROM gtb_product " .
//				 " WHERE  (id_pcategory IN (". intval($id_category). ") OR (-1 = " . intval($id_category) . ")) AND   match(vi_name, en_name, vi_description, en_description, code_pcategory, code ) against(".$this->db->escape($keyword)." IN BOOLEAN MODE)";
//		
		
	}
	
	function getItemsOrder($orderId){
		
        $orderId = intval($orderId);
        
		$sql = "SELECT d.*, p.vi_name, p.en_name 
				FROM gtb_order_detail d
				LEFT JOIN gtb_product p ON p.id_product = d.product_id
				WHERE  order_id = ". $orderId;
		
		$query = $this->db->query($sql);
		
		return  $query->result_array();
       	
	}
	
}

// EOF

